package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.AdvisoryCancelDto;
import com.csun.cmny.provider.dto.AdvisoryCancelQueryDto;
import com.csun.cmny.provider.model.domain.OmcAdvisory;
import com.csun.cmny.provider.model.domain.OmcAdvisoryCancel;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcAdvisoryCancelService;
import com.csun.cmny.provider.service.OmcAdvisoryService;
import com.csun.cmny.provider.validator.OmcAdvisoryCancelAddValidator;
import com.csun.cmny.provider.validator.OmcAdvisoryCancelEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/advisoryCancel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcAdvisoryCancelController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcAdvisoryCancelService omcAdvisoryCancelService;

    @Resource
    private OmcAdvisoryService omcAdvisoryService;

    /**
     * 取消预约-新增
     * @param: [advisoryCancelDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addAdvisoryCancel(@Validated(OmcAdvisoryCancelAddValidator.class) @RequestBody AdvisoryCancelDto advisoryCancelDto,
                               BindingResult br) {
        advisoryCancelDto.setGroupId(getLoginAuthDto().getGroupId());

        Integer result = saveAdvisoryCancel(advisoryCancelDto);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    /**
     * 取消预约-修改
     * @param: [advisoryCancelDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editAdvisoryCancel(@Validated(OmcAdvisoryCancelEditValidator.class) @RequestBody AdvisoryCancelDto advisoryCancelDto,
                                BindingResult br){
        advisoryCancelDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = saveAdvisoryCancel(advisoryCancelDto);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    /**
     * 取消预约-删除
     * @param: [advisoryCancelId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/deleteById/{advisoryCancelId}", method = RequestMethod.POST)
    public Wrapper deleteAdvisoryCancelId(@PathVariable Long advisoryCancelId) {

        // 校验giftId是否为空
        Preconditions.checkArgument(advisoryCancelId != null, cmnyMessageSource
                .getMessage("advisory.id.null", null));

        Integer result = omcAdvisoryCancelService.deleteAdvisoryById(advisoryCancelId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 咨询记录查询
     * @param: [advisoryQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryAdvisoryCancelListWithPage(@RequestBody AdvisoryCancelQueryDto advisoryCancelQueryDto) {

        advisoryCancelQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = omcAdvisoryCancelService.queryAdvisoryCancelListWithPage(advisoryCancelQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 根据床位id查询咨询记录信息
     *
     * @param: [bedId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/getOldNameByBedId/{bedId}", method = RequestMethod.POST)
    public Wrapper getOldNameByBedId(@PathVariable("bedId") Integer bedId) {

        OmcAdvisory omcAdvisory = new OmcAdvisory();
        omcAdvisory.setBedId(bedId);


        List<OmcAdvisory> select = omcAdvisoryService.select(omcAdvisory);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), select.get(0));
    }

    /**
     * 取消预约-保存
     * @param: [advisoryCancelDto]
     * @return: java.lang.Integer
     */
    public Integer saveAdvisoryCancel(AdvisoryCancelDto advisoryCancelDto) {
        OmcAdvisoryCancel omcAdvisoryCancel = new OmcAdvisoryCancel();
        BeanUtils.copyProperties(advisoryCancelDto, omcAdvisoryCancel);
        return omcAdvisoryCancelService.saveAdvisoryCancel(omcAdvisoryCancel, getLoginAuthDto());
    }
}